package shop.local.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import shop.local.domain.ShopVerwaltung;
import shop.local.domain.exceptions.FehlerhafteEingabeException;
import shop.local.domain.exceptions.UngueltigeEingabeException;
import shop.local.gui.RegisterFormPanel.RegisterListener;
import shop.local.valueobjects.Artikel;
import shop.local.valueobjects.Person;

public class PersonSearchPanel extends JPanel {
	
	private ShopVerwaltung shop;
	private Eshop eShop;
	private Person user;
	
	private JFrame frame = new JFrame();
	private JTextField searchField;
	private JButton searchButton;

	public PersonSearchPanel(JFrame aFrame, Person user) {
		this.user = user;
		
		try {
			shop = new ShopVerwaltung("SHOP");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		frame = aFrame;
		initialize();
	}

	private void initialize() {

		JLabel searchLabel = new JLabel("Name");
		JLabel emptyLabel = new JLabel("");
		searchField = new JTextField(20);
		searchButton = new JButton();

		searchField.setText("Nachname");
		searchField.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				searchFieldMouseClicked(evt);
			}
		});

		searchButton.setText("los!");
		searchButton.addActionListener(new SearchListener());

		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		buttonPanel.setBackground(new Color(255, 255, 255));
		buttonPanel.add(searchButton);

		setPreferredSize(new Dimension(300, 100));
		this.setLayout(new GridLayout(3, 3));
		this.setBackground(new Color(255, 255, 255));
		this.setBorder(BorderFactory
				.createTitledBorder("Personensuche"));

		this.add(searchLabel);
		this.add(searchField);
		this.add(emptyLabel);
		this.add(buttonPanel);
	}

	protected void searchFieldMouseClicked(MouseEvent evt) {
		if (searchField.getText().equals("Nachname")) {
			searchField.setText("");
		} else if (searchField.getText().equals("")) {
			searchField.setText("Nachname");
		}
		searchField.selectAll();
	}
	
	class SearchListener implements ActionListener {
		
		@Override
		public void actionPerformed(ActionEvent ae) {
			if (ae.getSource().equals(searchButton)) {
				String nachname = searchField.getText();
				if (nachname.isEmpty() || nachname.equals("Nachname")) {
					try {
						throw new FehlerhafteEingabeException(
								"Bitte Nachnamen eingeben!");
					} catch (FehlerhafteEingabeException e) {
						JOptionPane.showMessageDialog(null, e.getMessage(),
								"Fehler", JOptionPane.ERROR_MESSAGE);
						e.printStackTrace();
					}
				} else {
					eShop = new Eshop("SHOP", user);
//					System.out.println(user);
					eShop.user = user;
					java.util.List<Person> person = null;
					person = shop.sucheNachNachname(nachname);
					eShop.personenSuche(person);
					frame.dispose();
				}
			}
		}
	}

}
